import {CHANGE_LOGIN} from './constants'
import Cookie from 'js-cookie'

const changeLogin = (login) => ({
	type: CHANGE_LOGIN,
	login
})

export const isLogin = () => {
	return (dispatch, getState, axios) => {
		return axios.get('/api/isLogin').then((res) => {
			dispatch(changeLogin(res.data.data.login))
		})
	}
}


export const login = () => {
	return (dispatch, getState, axios) => {
		return axios.get('/api/login').then((res) => {
			Cookie.set('login', 'true', {expires: 7})
			dispatch(changeLogin(true))
		})
	}
}

export const logout = () => {
	return (dispatch, getState, axios) => {
		return axios.get('/api/logout').then((res) => {
			Cookie.set('login', '')
			dispatch(changeLogin(false))
		})
	}
}